package com.web.netalbum.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.web.netalbum.bean.Photo;
import com.web.netalbum.dao.PhotoDao;
import com.web.netalbum.util.DBUtil;

public class PhotoDaoImpl implements PhotoDao {
	private QueryRunner qr = new QueryRunner(DBUtil.getDataSource());

	@Override
	public Photo findPhotoById(String id) {
		try {
			return qr.query("select * from photo where id=?",
					new BeanHandler<Photo>(Photo.class), id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<Photo> findAllPhotoById(String aid) {
		try {
			return qr.query("select * from photo where aid=?",
					new BeanListHandler<Photo>(Photo.class), aid);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public void deletePhoto(String id) {
		try {
			qr.update("delete from photo where id=?", id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void savePhoto(Photo photo) {
		try {
			qr.update(
					"insert into photo(name,path,keyword,click,praise,contentTime,uid,aid) values(?,?,?,?,?,?,?,?)",
					photo.getName(), photo.getPath(), photo.getKeyword(),
					photo.getClick(), photo.getPraise(),
					photo.getContentTime(), photo.getUid(), photo.getAid());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void increaseClick(int id, int click) {
		try {
			qr.update("update photo set click= ? where id=?", click + 1, id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public void increasePrase(int id, int praise) {
		// TODO Auto-generated method stub
		try {
			qr.update("update photo set praise= ? where id=?", praise+1, id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public List<Photo> findAllPhotoByAlbumId(int id, int count) {
		try {
			return qr.query("select *from photo where aid=? order by contentTime desc limit 0,"+count, new BeanListHandler<Photo>(Photo.class),id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<Photo> findPhotoByKeyword(String string) {
		try {
			return qr.query("select *from photo where keyword like ?",new BeanListHandler<Photo>(Photo.class), "%"+string+"%");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<Photo> findPhotosByAlbumId(int id) {
		
		try {
			return qr.query("select *from photo where aid=?", new BeanListHandler<Photo>(Photo.class),id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<Photo> findPhotos(int i) {
		// TODO Auto-generated method stub
		try {
			return qr.query("select *from photo order by contentTime desc limit 0,"+i, new BeanListHandler<Photo>(Photo.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<Photo> findPhotosByAidAndUid(int aid, String uid) {
		
		try {
			return qr.query("select *from photo where aid=? and uid=?", new BeanListHandler<Photo>(Photo.class),aid,uid);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<Photo> findAllPhotosByUserId(String id) {
		try {
			return qr.query("select *from photo where uid=? order by contentTime desc", new BeanListHandler<Photo>(Photo.class),id);
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public void savePhotos(Object[][] args) {
		try {
			qr.batch("insert into photo(name,path,contentTime,uid,aid) values(?,?,?,?,?)", args);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	
}
